package com.ctshk.rpc.finance.mapper;


import com.ctshk.rpc.finance.base.CustomBaseMapper;
import com.ctshk.rpc.finance.dto.bill.BillGroupRecordExportDTO;
import com.ctshk.rpc.finance.dto.bill.BillGroupRecordSumDTO;
import com.ctshk.rpc.finance.entity.FinanceBillGroupRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 团期认款记录 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-06-22
 */
public interface FinanceBillGroupRecordMapper extends CustomBaseMapper<FinanceBillGroupRecord> {

    /**
     * 根据日期获取
     * @param date
     * @return
     */
    @Select("SELECT id,group_type,subect_type,summary,currency,subect_number,type,adjust_type,exchange_rate,amount,hkd_amount,sale_dept_code,cost_code,add_time,create_date,sale_order_id\n" +
            "FROM t_finance_bill_group_record\n" +
            "WHERE create_date = #{create_date} AND type = #{type} AND is_deleted = 0\n" +
            "ORDER BY gmt_create DESC")
    List<BillGroupRecordExportDTO> queryByDate(@Param("create_date") LocalDate date,@Param("type") int type);

    /**
     * 根据日期获取订单ID列表
     * @param date
     * @return
     */
    @Select("SELECT sale_order_id\n" +
            "FROM t_finance_bill_group_record\n" +
            "WHERE create_date = #{create_date} AND type = #{type} AND is_deleted = 0\n" +
            "GROUP BY sale_order_id ORDER BY gmt_create")
    List<Long> queryOrderIdListByDate(@Param("create_date")LocalDate date, @Param("type") int type);

    /**
     * 订单收退款统计
     * @param date
     * @param idList
     * @return
     */
    @Select("<script>"+
            "SELECT id,group_type,subect_type,summary,currency,subect_number,exchange_rate,sale_dept_code,cost_code,add_time,create_date,sale_order_id,\n" +
            "SUM(IF(adjust_type='1',hkd_amount,0)) AS collectionAmount,SUM(IF(adjust_type='2',hkd_amount,0)) AS refundAmount\n" +
            "FROM t_finance_bill_group_record\n" +
            "WHERE create_date = #{create_date} AND type = 0 AND is_deleted = 0\n" +
            "AND sale_order_id IN "+
                "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
                "#{id}\n" +
                "</foreach> "+
            "GROUP BY subect_type ORDER BY gmt_create DESC"+
            "</script>")
    List<BillGroupRecordSumDTO> sumByDate(@Param("create_date")LocalDate date, @Param("list") List<Long> idList);
}
